import { Component, OnInit,ChangeDetectorRef } from '@angular/core';
import { Router } from '@angular/router'
import { AlertController } from '@ionic/angular';
import { StorageService } from '../service/storage.service'
import { CommonService } from '../service/common.service'
import { EventService } from '../service/event.service'

@Component({
  selector: 'app-tab4',
  templateUrl: './tab4.page.html',
  styleUrls: ['./tab4.page.scss'],
})
export class Tab4Page implements OnInit {

  constructor(private router:Router,private storage:StorageService,private common:CommonService,public alertController: AlertController,
    private eventService:EventService) { }

  public mymills:any;
  public sysCount:number = 0;
  public version:string = 'V1.0.0'

  // 定义登录信息
  public userInfo:any; 
  
  ngOnInit() {
    this.userInfo = this.common.getInfo()

    this.mymill()
    this.unreadCount()

    //监听 通知数量 事件
    this.eventService.event.on('numbercount',()=>{
      console.log('实时数据')
      this.sysCount = this.sysCount -1
      // this.unreadCount()
    })

    //监听 通知数量 事件  
    this.eventService.event.on('hashrate',()=>{
      this.mymill()
      console.log('算力更新')
    })

    //监听 信息缓存 事件
    this.eventService.event.on('useraction',()=>{
      // console.log(11111111111111111)
      // 传ID更新储存
      var api = '/tbr/getUserInfo'
      var params = {'userId': this.common.getInfo().userId}
      
      this.common.post(api,params,false).then((response:any)=>{
        if(response.code == 200){
          // 保存信息
          this.storage.set('PersonalInfo',response.data)
          this.userInfo = this.common.getInfo()
        }
      })
    })
  }

  onUserInfo(){
    this.router.navigateByUrl("mine/info")
  }

  // 系统通知
  sysInfo(){
    this.router.navigateByUrl("list/notice")
  }

  // 刷新
  doRefresh(event) {
    setTimeout(() => {
      this.common.getIdInfo()
      this.ngOnInit()
      // this.mymill()
      // this.unreadCount()
      // this.common.getIdInfo()
      event.target.complete();
    }, 1500);
  }

  // 退出登录
  onOut(){
    console.log('out')
    this.presentAlertUrl("您确定注销当前账号？")
  }

  // 弹出框
  async presentAlertUrl(message:string) {
    const alert = await this.alertController.create({
        // cssClass: 'my-custom-class',
        header: '提示',
        // subHeader: 'Subtitle',
        message: message,
        buttons: [
          {
            text: '取消',
            role: 'cancel'
          },
          {
            text: '确定',
            handler: (blah) => {
              this.storage.remove('userInfo')
              this.storage.remove('PersonalInfo')
              this.router.navigateByUrl("login")
              this.common.presentToast('登录已退出，请重新登录')
              // location.reload()
            }
          }
        ]
    });
    await alert.present();
  }

  // 我的ipfs矿机
  mymill(){
    var api = '/tbr/experience/getDateList'

    var params = {userId:this.storage.get('PersonalInfo').userId}

    this.common.post(api,params,false).then((res:any)=>{
      console.log(res)
      if(res.code == 200){
        this.mymills = res.data
        this.mymills[0].img = 'assets/img/ipfs.png'
        this.mymills[1].img = 'assets/img/chia.png'
        this.mymills[2].img = 'assets/img/swarm.png'
      }
    })
  }

  // 系统通知数量
  unreadCount(){
    var api = '/tbr/siteNotice/unreadCount'

    this.common.get(api).then((res:any)=>{
      // console.log(res)
      if(res.code == 200){
        this.sysCount = res.data
      }
    })

  }

  

}
